using System;
using System.Linq;
using UnityEngine;

namespace NodeCanvas.Variables
{
	[AddComponentMenu("")]
	public class EnumData : VariableData
	{
		private enum DefaultEnum
		{
			One = 0,
			Two = 1,
			Three = 2,
			Four = 3,
			Five = 4
		}

		public Enum value;

		public string stringValue;

		[SerializeField]
		private string _typeName = typeof(DefaultEnum).AssemblyQualifiedName;

		public override Type varType
		{
			get
			{
				return type;
			}
		}

		private Type type
		{
			get
			{
				return Type.GetType(_typeName);
			}
			set
			{
				_typeName = value.AssemblyQualifiedName;
				stringValue = Enum.GetNames(value)[0];
			}
		}

		public override object objectValue
		{
			get
			{
				return Enum.Parse(type, stringValue);
			}
			set
			{
				if (value != null && typeof(Enum).NCIsAssignableFrom(value.GetType()) && type != value.GetType())
				{
					type = value.GetType();
				}
				if (value.GetType() == typeof(string))
				{
					if (Enum.GetNames(type).Contains(value))
					{
						stringValue = (string)value;
					}
					else
					{
						Debug.LogError(string.Format("{0} is not a valid name of the {1} enum type", value, type));
					}
				}
				else
				{
					stringValue = Enum.GetName(type, value);
					this.value = (Enum)value;
				}
			}
		}
	}
}
